<script type="text/javascript">
var $dg_role ;
$(function(){
	$dg_role = $("#dg_role").datagrid({
        url: "${ctx}/manager/system/role/datagrid",
        idField: "id", fit: true, 
        border: false, striped:true, pageSize: 30, pageList: [30,40,50,100,200,500,1000], 
        pagination: true, rownumbers: true, remoteSort: true, 
        sortName: 'sorting', sortOrder: 'asc', queryParams: {},
        frozenColumns: [[
            { field: "ck", checkbox: true },
            { field: "id", title: "ID", width: 80, sortable: true, hidden: true }
        ]],
        columns: [[ 
            { field: "name", title: "名称", width: 120 },
            { field: "description", title: "描述", width: 250 },
        ]],
        onLoadSuccess: function(){
        	$.fn.datagrid.extensions.onLoadSuccess.apply(this, arguments);
        	$dg_role.datagrid('unselectAll');$dg_role.datagrid('uncheckAll');$dg_role.datagrid('clearSelections');
        },
        enableHeaderClickMenu: true, enableHeaderContextMenu: true, enableRowContextMenu: false
    });
    
});	

function getRoleIds() {
	var rows = $dg_role.datagrid('getChecked');
	var ids = [];
	if (rows.length > 0) {
		for (var i = 0; i < rows.length; i++) {
			if(undefined != rows[i].id && ""!=rows[i].id) {
				ids.push(rows[i].id);
			}
		}
	}
	return ids ;
}	

//提交表单数据
var submitNow = function($d, $dg, flag) {
	var ids = getRoleIds() ;
	if(ids.length == 0) {$.easyui.loaded();alertify.warning("请选择一条记录！");return;}
	var data = {"ids": $("#ids").val(), "roleIds": ids.join(",")} ;
	
	$.post("${ctx}/manager/system/user/addRoleToUser", data, function(result) {
		if (result.status) {
			$dg.datagrid('reload');alertify.success(result.message);$.easyui.loaded();
			if(flag==undefined||flag==false){$d.dialog("close") ;}
		} else {
			$.easyui.loaded();$.messager.alert("错误", result.message, "error");
		}
	}, 'json').error(function() { $.easyui.loaded(); });
};

//验证表单
var submitForm = function($d, $dg, flag) { 
	if($('#form').form('validate')) {
		$.easyui.loading({ msg: "数据提交中，请稍等..." });
		submitNow($d, $dg, flag) ;
	}
};
</script>

<div id="dg_role">
	<input type="hidden" id="ids"/>
</div>

